<template>
  <my-drawer ref="myRef" title="分组详情" @submit="submit" :disabled="false" width="30%">
    <el-form :model="drawerProps.row" :rules="rules" ref="formRef" label-width="100">
      <el-form-item label="分组名称" prop="name">
        <el-input v-model="drawerProps.row.name"></el-input>
      </el-form-item>
      <el-form-item label="字体颜色">
        <el-color-picker v-model="drawerProps.row.textColor"></el-color-picker>
      </el-form-item>
      <el-form-item label="边框颜色">
        <el-color-picker v-model="drawerProps.row.borderColor"></el-color-picker>
      </el-form-item>
      <el-form-item label="排序">
        <el-input-number v-model="drawerProps.row.sort" :min="1" :max="100"></el-input-number>
      </el-form-item>
      <el-form-item label="预览" v-show="drawerProps.row.name">
        <span class="px-5px" :style="{ border: `1px solid ${drawerProps.row.borderColor}`, color: drawerProps.row.textColor }">{{
          drawerProps.row.name
        }}</span>
      </el-form-item>
    </el-form>
  </my-drawer>
</template>

<script setup lang="ts">
import MyDrawer from "@/components/MyDrawer/index.vue";
import { ref } from "vue";
import { ArticleGroup } from "@/api/interface/article";
import { ElMessage, FormInstance, FormRules } from "element-plus";
import { JustCanChinese } from "@/utils/eleValidate";

const myRef = ref<InstanceType<typeof MyDrawer> | null>();

interface DrawerProps {
  row: any;
  api?: (params: any) => Promise<any>;
  getTable?: () => Promise<any>;
}

const initName: ArticleGroup.Option = { name: "", sort: 100 };

const drawerProps = ref<DrawerProps>({ row: JSON.parse(JSON.stringify(initName)) });

const rules: FormRules = {
  name: [{ required: true, message: "名称不可为空" }, { min: 2, max: 4, message: "字符在2-4之间" }, { validator: JustCanChinese }]
};

const acceptParams = (params: DrawerProps) => {
  drawerProps.value = params;
  myRef.value?.acceptParams();
};

const formRef = ref<FormInstance>();

const submit = async () => {
  if (!formRef.value) return;
  formRef.value.validate(async valid => {
    if (!valid) return;
    try {
      await drawerProps.value.api!(drawerProps.value.row);
      ElMessage.success("成功");
      await drawerProps.value.getTable!();
      myRef.value?.close();
    } catch (error) {}
  });
};

defineExpose({
  acceptParams
});
</script>

<style></style>
